package com.compass.doctor.ui.setting

import android.os.Bundle
import android.view.View
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.compass.doctor.databinding.ActivityExpensesSettingBinding
import com.compass.doctor.ui.setting.adapter.ExpensesAdapter
import com.compass.doctor.ui.setting.viewmodel.ConsultSettingViewModel
import com.compass.doctor.base.BaseVBActivity
import com.compass.framework.constant.KEY_CONSULT_TYPE
import com.compass.framework.constant.PHONE_LADDER
import com.compass.framework.constant.TEXT_LADDER
import com.compass.framework.constant.VIDEO_LADDER
import com.compass.framework.ext.onClick
import com.compass.framework.model.Dictionaries
import com.compass.framework.utils.LogUtil
import com.compass.framework.utils.MoneyInputFilter

/**
 * Description： 设置接诊资费金额
 * Date:2023/12/18 17:11
 */
class ExpensesSettingActivity : BaseVBActivity<ActivityExpensesSettingBinding>() {

    private lateinit var expensesAdapter: ExpensesAdapter
    private var dictionaries: MutableList<Dictionaries> = ArrayList()
    private val consultSettingViewModel: ConsultSettingViewModel by viewModels()

    private var isOther: Boolean = false
    private var mPrice: String = "0"
    private var type: Int = 1 //1 图文 2电话 3视频
    override fun initView(savedInstanceState: Bundle?) {
        type = intent.getIntExtra(KEY_CONSULT_TYPE, 1)
        expensesAdapter = ExpensesAdapter()
        mBinding.rcyConsulting.apply {
            layoutManager = LinearLayoutManager(this@ExpensesSettingActivity)
            adapter = expensesAdapter

        }
        expensesAdapter.onItemClickListener = { _: View, position: Int ->
            onItemClick(position)
        }
        val filter = MoneyInputFilter()
        filter.setMaxValue(1000.00)
        mBinding.tvPrice.filters = arrayOf(filter)
        mBinding.tvConfirm.onClick {

            for (i in dictionaries) {
                if (i.isSelect) mPrice = i.value
            }
            LogUtil.d("http_data==price$mPrice")

            if (isOther) {
                mPrice = mBinding.tvPrice.text.toString()
                if (mPrice.isEmpty()) {
                    showToast("请选择或者输入资费金额")
                    return@onClick
                }
                try {
                    var doublePrice = mPrice.toDouble()
                    if (doublePrice > 0) {
                        intent.putExtra("price", mPrice.toDouble())
                        setResult(RESULT_OK, intent)
                        finish()
                    } else {
                        showToast("资费金额必须大于0")
                        return@onClick
                    }
                } catch (e: Exception) {
                    showToast("请输入正确的金额")
                    return@onClick
                }

            } else {
                intent.putExtra("price", mPrice.toDouble())
                setResult(RESULT_OK, intent)
                finish()
            }

        }
    }

    override fun initData() {
        var typeValue = ""
        when (type) {
            1 -> {
                typeValue = TEXT_LADDER
            }

            2 -> {
                typeValue =PHONE_LADDER
            }

            3 -> {
                typeValue = VIDEO_LADDER
            }
        }

        consultSettingViewModel.getDictionaries(typeValue)
            .observe(this) {
                it?.let {
                    it.add(Dictionaries("其他金额", "其他"))
                    dictionaries.addAll(it)
                    expensesAdapter.setData(dictionaries)
                }
            }
    }

    private fun onItemClick(position: Int) {

        for (i in dictionaries.indices) {
            dictionaries[i].isSelect = false
            if (i == position) {
                dictionaries[i].isSelect = true
                if (dictionaries[i].value == "其他") {
                    isOther = true
                    mBinding.llOther.visibility = View.VISIBLE
                } else {
                    isOther = false
                    mBinding.llOther.visibility = View.GONE
                }
            }

        }
        expensesAdapter.notifyDataSetChanged()
    }
}